package db

import (
	"time"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

const (
	typeMysql = "mysql"
)

var (
	GlobalDB *gorm.DB
)

func InitDBConn(dns string, maxIdel, maxLifeTime, maxIdleTime, maxOpen int) {
	var err error
	GlobalDB, err = gorm.Open(typeMysql, dns)
	if err != nil {
		panic(err)
	}
	GlobalDB.DB().SetConnMaxLifetime(time.Duration(maxLifeTime) * time.Second)
	GlobalDB.DB().SetConnMaxIdleTime(time.Duration(maxIdleTime) * time.Second)
	GlobalDB.DB().SetMaxOpenConns(maxOpen)
	GlobalDB.DB().SetMaxIdleConns(maxIdel)
	GlobalDB.LogMode(true)

}
